<?php
/**
 * Created by leiying
 * Date: 2017/11/16
 * Time: 16:13
 */

/**分页获取订单列表
 * @param $uid
 * @param $page
 * @param $page_size
 * @param int $type 0-全部 1-待付款 2-待发货 3-待收货 4-待评价
 * @return array
 * @author leiying
 */
function get_orders($uid,$page,$page_size,$type=0){

    switch($type){
        case '1':
            $sql_count = 'SELECT count(*) FROM '. $GLOBALS['db']->table("order").'WHERE `user_id`='.$uid;
            break;
        case '2':
            $sql_count = 'SELECT count(*) FROM '. $GLOBALS['db']->table("order").'WHERE `user_id`='.$uid;

            break;
        case '3':
            $sql_count = 'SELECT count(*) FROM '. $GLOBALS['db']->table("order").'WHERE `user_id`='.$uid;

            break;
        case '4':
            $sql_count = 'SELECT count(*) FROM '. $GLOBALS['db']->table("order").'WHERE `user_id`='.$uid;

            break;
        default:
            $sql_count = 'SELECT count(*) FROM '. $GLOBALS['db']->table("order").'WHERE `user_id`='.$uid;
            $sql = 'SELECT order_id,shipping_id,shipping_fee,order_surplus,order_status,shipping_status,comment_status,pay_status FROM '. $GLOBALS['db']->table("order").'WHERE `user_id`='.$uid.'   ORDER BY `add_time` DESC';
            break;
    }
    $count=$GLOBALS['db']->getOne($sql_count);
    $page_count = ($count > 0) ? intval(ceil($count / $page_size)) : 1;

    $info = $GLOBALS['db']->selectLimit($sql, $page_size, ($page-1) * $page_size);

    $list = array();
    $arr = array();

    while ($row=$GLOBALS['db']->fetchRow($info)) {
        $arr["order_id"] = $row["order_id"];
        $arr["order_surplus"] = $row["order_surplus"];
        $arr["shipping_id"] = $row["shipping_id"];
        $arr["shipping_fee"] = $row["shipping_fee"];
        if($row["order_status"] == 2){//0(待收货)  1（已收货） 2（订单关闭）
            $arr["status_desc"] = "交易关闭";
        }else{
            if($row["pay_status"] == 0){//0（待付款/未支付） 1（已付款/已支付）
                $arr["status_desc"] = "等待买家付款";
            }else{
                if($row["shipping_status"] == 0){//0(待发货/未发货)  1（已发货）
                    $arr["status_desc"] = "等待卖家发货";
                }else{
                    if($row["order_status"] == 0){
                        $arr["status_desc"] = "待收货";
                    }else{
                        if($row["comment_status"] == 0){
                            $arr["status_desc"] = "待评价";
                        }else{
                            $arr["status_desc"] = "交易完成";
                        }
                    }
                }
            }
        }

        $sql = 'SELECT b.goods_id,b.goods_name,b.goods_img,b.goods_number,b.goods_price,b.goods_attr,b.goods_type FROM '. $GLOBALS['db']->table("order").' AS a RIGHT JOIN '. $GLOBALS['db']->table("order_goods").' as b on a.order_id=b.order_id WHERE a.order_id='.$row["order_id"].'  ORDER BY a.add_time DESC,b.rec_id ASC';
        $res = $GLOBALS['db']->getAll($sql);
        $arr["sum_number"] = 0;
        foreach($res as &$v){
            $arr["sum_number"] += $v["goods_number"];
            $v["goods_price"] = $v["goods_price"]*$v["goods_number"];
            $v["goods_img"] = GZ_Api::get_url($v["goods_img"]);
        }
        $arr["goods_list"] = $res;
        $list[] = $arr;
    }


    $pager = array(
        "total"  => $count,
        "count"  => count($list),
        "more"   => $page < $page_count ? 1 : 0
    );

    $res = array('list' =>$list, 'pager' => $pager);

    return $res;
}









